<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>
<body>
  
</body>
<script>
// 在扫描整个文件夹之前，可以先移除某一个具体的文件
class Folder {
  constructor(name) {
    this.name = name
    this.parent = null
    this.files = []
  }
  add(file) {
    file.parent = this
    this.files.push(file)
  }
  scan() {
    console.log('开始扫描文件夹' + this.name)
    this.files.map(file => file.scan())
  }
  remove() {
    // 根节点或者树外的游离节点
    if(!this.parent) return
    for(let files = this.parent.files, i = files.length-1; i >= 0; i--) {
      if(files[i] = this) {
        files.splice(i, 1)
      }
    }
  }
}

class File {
  constructor(name) {
    this.name = name
    this.parent = null
  }
  add() {
    throw new Error('文件下面不能再添加文件')
  }
  scan() {
    console.log('开始扫描文件' + this.name)
  }
  remove() {
    // 根节点或者树外的游离节点
    if(!this.parent) return
    for(let files = this.parent.files, i = files.length-1; i >= 0; i--) {
      if(files[i] = this) {
        files.splice(i, 1)
      }
    }
  }
}

let folder = new Folder('学习资料')
let folder1 = new Folder('js')
let file1 = new File('vue文件')

folder1.add(new File('js设计模式'))
folder.add(file1)

folder1.remove()
folder.scan()
</script>
</html>